//
//  MFWServiceModel.h
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-8-1.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "MFWSdk.h"

@protocol MFWServiceHandler<NSObject>

/**
 *	default return NO, sub-class should overide to deal with his own service
 *
 *	@param	rawData	http response raw data
 *	@param	jsonObj	http response pased json object
 *	@param	task	http task
 *
 *	@return	deal service sucessfully or not
 */
- (BOOL)processServiceSuccRawData:(NSData*)rawData parsedJson:(MFWJson*)jsonObj forTask:(MFWHttpTask*)task;


@end

@protocol DataModelInterface <NSObject>

@required
- (BOOL)canGetMoreForDataId:(NSString*)dataId;
@end



@interface MFWServiceModel : MFWDataModel<MFWServiceHandler, DataModelInterface>
{
    NSMutableDictionary *_dataStatusDic;
}
/**
 *	Default return NO
 *
 *	@return
 */
- (BOOL)bSupportPersistentCache:(NSString*)dataId;

/**
 *	业务数据接口返回的json数据中列表型数据的key
 *  default return @"message"
 *
 *	@return	key string in json object
 */
- (NSString*)keyOfListData;

/**
 *	列表型json数据，该数据是json array, 是@selector(keyOfListData)返回的key对应的value,
 *  fetching data using [self jsonDataForDataId:dataId].
 *
 *	@param	dataId	model中的dataId
 *
 *	@return	json array
 */
- (MFWJson*)listJsonForDataId:(NSString*)dataId;
/**
 *	@see [self listJsonForDataId:mfwDefaultDataId]
 *
 *	@return	json array
 */
- (MFWJson*)listJson;

- (MFWDataStatus*)dataStatusForDataId:(NSString*)dataId;

/**
 *  append an item data to list array
 *
 *  @param item 
 *  @param dataId
 */
- (BOOL)appendListItem:(id)item forDataId:(NSString*)dataId;


/**
 *  remove list item
 *
 *  @param indexSet item index set
 *  @param dataId   dataId
 */
- (void)removeListItemAntIndexSet:(NSMutableIndexSet*)indexSet forDataId:(NSString*)dataId;


/**
 *  remove all list item for dataId
 *
 *  @param dataId   dataId
 */
- (void)removeListItemForDataId:(NSString*)dataId;


@end

@interface MFWServiceModel(Private)
/**
 *	deal with data-caching, the json data is the concrete service related data
 *  This function called after @selector(processSuccRawData:parsedJson:forTask:).
 *  Sub-class could override it to implements its' own service appeal.
 *
 *	@param	rawData http response raw data
 *	@param	jsonObj concrete service data (the json value of key "data")
 *	@param	task    http request task, involves data id and other request informations
 */
- (void)_processPersistentCacheRawData:(NSData*)rawData parsedJson:(MFWJson*)jsonObj forTask:(MFWHttpTask*)task;

/**
 *	this methods will auto called when [self jsonDataForDataId:dataId] returns nil and [self bSupportPersistentCache]
 *  returns YES. This methods does the oppsite things of @selector(_processPersistentCacheRawData:parsedJson:forTask)
 *
 *	@param	dataId	data id
 *
 *	@return	parsed json object from persistent storage data
 */
- (MFWJson*)_loadCacheDataForDataId:(NSString*)dataId;

- (BOOL)processCacheData:(MFWJson*)cacheData forDataId:(NSString*)dataId;
@end
